/**
 * 
 */
package com.simplecms.util;

import java.security.MessageDigest;

import com.jfinal.log.Logger;
import com.simple.user.UserInterceptor;

/**
 * @author hokin.jim
 *
 */
public class MD5Util {
	private static Logger logger = Logger.getLogger(UserInterceptor.class);
	//=============================================================
		//MD5Util单例
		private static MD5Util md5=null;
		private MD5Util(){};
		public static MD5Util getInstance(){
			if(md5==null){
				md5= new MD5Util();
			}
			return md5;
		}
	//=============================================================
	/**
	 * MD5加密
	 * @param source
	 * @return
	 */
	public  String simpleMD5(String source) {  
        
	    StringBuffer sb = new StringBuffer(32);  
	    try {  
	        MessageDigest md    = MessageDigest.getInstance("MD5");  
	        byte[] array        = md.digest(source.getBytes("utf-8"));  
	              
	        for (int i = 0; i < array.length; i++) {  
	            sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).toUpperCase().substring(1, 3));  
	        }  
	    } catch (Exception e) {  
	        logger.error("Can not encode the string '" + source + "' to MD5!", e);  
	        return null;  
	    }  
	          
	    return sb.toString();  
	}  
	/**
	 * MD5加私钥多次加密
	 * @param source
	 * @return
	 */
	public  String getMD5ByPrivateKey(String source) {  
	    String privateKey ="zhanxj";
	    source=simpleMD5(source)+privateKey;
	    return simpleMD5(source);  
	}  
}
